/*
 * @Author: fah 1742484532@qq.com
 * @Date: 2024-08-28 16:46:26
 * @LastEditors: fah 1742484532@qq.com
 * @LastEditTime: 2024-09-13 14:23:23
 * @FilePath: \livetree-admin-server\src\users\users.service.ts
 * @Description: 指飞部-企业数字化管理系统
 * @version: 1.0
 */
/*
 * @Author: fah 1742484532@qq.com
 * @Date: 2024-08-28 16:46:26
 * @LastEditors: fah 1742484532@qq.com
 * @LastEditTime: 2024-08-31 10:29:02
 * @FilePath: \livetree-admin-server\src\users\users.service.ts
 * @Description: 指飞部-企业数字化管理系统
 * @version: 1.0
 */
import { Injectable } from '@nestjs/common';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { User } from './entities/user.entity';
import { responseMessage } from 'src/utils';

@Injectable()
export class UsersService {
  constructor(
    @InjectRepository(User)
    private readonly usersRepository,
  ) {}

  create(createUserDto: CreateUserDto) {
    console.log('createUserDto', createUserDto);
    const user = new User();
    user.name = createUserDto.name;
    user.age = createUserDto.age;
    console.log('user', user);
    this.usersRepository.save(user);
    return responseMessage(null);
  }

  async registerByWechat(userInfo) {
    const { nickname, open_id, avatar, sex } = userInfo;
    console.log('nickname, open_id, avatar', nickname, open_id, avatar);
    const newUser = await this.usersRepository.create({
      nickname,
      open_id,
      avatar,
      user_type: 0,
      sex
    });
    // console.log('newUser', newUser);
    return await this.usersRepository.save(newUser);
  }

  async findByOpenid(open_id) {
    console.log("findByOpenid", open_id)
    // return true
    return await this.usersRepository.findOne({ where: { open_id } });
  }

  findAll() {
    return `This action returns all users`;
  }

  findOne(id: number) {
    return `This action returns a #${id} user`;
  }

  update(id: number, updateUserDto: UpdateUserDto) {
    return `This action updates a #${id} user`;
  }

  remove(id: number) {
    return `This action removes a #${id} user`;
  }
}
